package com.dao;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.entity.HistoryItem;

import android.content.Context;
import android.util.Log;

public class HistoryDAO {
	public static String RECENT_FILE = "recent.bin";
	private static String fileName = "recent.bin";
	private Context context;

	public HistoryDAO(Context context) {
		// TODO Auto-generated constructor stub
		this.context = context;
	}

	public Boolean add(HistoryItem historyItem) {
		try {
			ArrayList<HistoryItem> listHistoryItems=loadAll();
			FileOutputStream fo = context.openFileOutput(fileName,
					Context.MODE_WORLD_WRITEABLE);
			BufferedOutputStream bo = new BufferedOutputStream(fo);
			ObjectOutputStream obo = new ObjectOutputStream(bo);
			
			if(listHistoryItems!=null&&listHistoryItems.size()>0)
			{
				for(HistoryItem historyItem2:listHistoryItems)
					obo.writeObject(historyItem2);
			}
			Log.d("-----------message-------", "loaded");
			obo.writeObject(historyItem);
			obo.close();
			bo.close();
			fo.close();
			return true;
		} catch (Exception ex) {
			return false;
		}
	}

	public ArrayList<HistoryItem> loadAll() {
		ArrayList<HistoryItem> listHistoryItems = new ArrayList<HistoryItem>();
		try {
			FileInputStream fi = context.openFileInput(fileName);
			BufferedInputStream bi = new BufferedInputStream(fi);
			ObjectInputStream oi = new ObjectInputStream(bi);
			HistoryItem historyItem;
			while (bi.available() > 0) {
				historyItem = (HistoryItem) oi.readObject();
				if (historyItem != null)
					listHistoryItems.add(historyItem);
			}
			Log.d("------meassge-------",String.valueOf(listHistoryItems.size()));
			oi.close();
			bi.close();
			fi.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return listHistoryItems;
	}
	public Boolean deleteAll(){
		FileOutputStream fo;
		try {
			fo = context.openFileOutput(fileName,Context.MODE_WORLD_WRITEABLE);
			BufferedOutputStream bo=new BufferedOutputStream(fo);
	   		ObjectOutputStream obo=new ObjectOutputStream(bo);
	   		obo.close();
	        bo.close();
	   		return true;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}
	public Boolean delete(HistoryItem locate){
		ArrayList<HistoryItem> templist=new ArrayList<HistoryItem>(loadAll());
		Log.d("---messeage----","locate:"+new SimpleDateFormat("yyMMddHHmmss").format(locate.getDateCreate()));
		deleteAll();
		for(HistoryItem locate2:templist){
			Log.d("---messeage----","locate2:"+new SimpleDateFormat("yyMMddHHmmss").format(locate2.getDateCreate()));
			if(locate.getDateCreate().compareTo(locate2.getDateCreate())!=0)
				add(locate2);
			else
				Log.d("----message--","deleted");
		}
		return true;
	}
	
	public  void deleteDuringTime(Date date){
		DateFormat dateFormat=new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
		Log.d("---","-------compare date:"+dateFormat.format(date));
		try {
			List<HistoryItem> listHistoryItems=loadAll();
			List<HistoryItem> listTemp=new ArrayList<HistoryItem>();
			FileOutputStream fo = context.openFileOutput(fileName,
					Context.MODE_WORLD_WRITEABLE);
			BufferedOutputStream bo = new BufferedOutputStream(fo);
			ObjectOutputStream obo = new ObjectOutputStream(bo);
			
			for(HistoryItem historyItem:listHistoryItems){
				if(historyItem.getDateCreate().before(date))
				{
					Log.d("----","da luu lai:"+dateFormat.format(historyItem.getDateCreate()));
					listTemp.add(historyItem);
				}
				else
					Log.d("----","da xoa:"+dateFormat.format(historyItem.getDateCreate()));
			}
			if(listTemp!=null&&listTemp.size()>0)
			{
				for(HistoryItem historyItem2:listTemp)
					obo.writeObject(historyItem2);
			}
			obo.close();
			bo.close();
			fo.close();
		} catch (Exception ex) {
		}
	}
}
